package impl

import (
	"time"

	"gitee.com/go-course/go8/projects/devcloud/cmdb/apps/secret"
	"github.com/rs/xid"
)

func New(req *secret.CreateSecretRequest) *Secret {
	return &Secret{
		Id:                  xid.New().String(),
		CreateAt:            time.Now().Unix(),
		CreateSecretRequest: req,
	}
}

func NewSecretSet() *SecretSet {
	return &SecretSet{
		Items: []*Secret{},
	}
}

type SecretSet struct {
	Items []*Secret
}

func (s *SecretSet) SecretSet() *secret.SecretSet {
	set := secret.NewSecretSet()
	for i := range s.Items {
		set.Items = append(set.Items, s.Items[i].Secret())
	}

	return set
}

type Secret struct {
	Id       string `json:"id"`
	CreateAt int64  `json:"create_at"`
	*secret.CreateSecretRequest
}

func (s *Secret) TableName() string {
	return "secret"
}

func (s *Secret) Secret() *secret.Secret {
	return &secret.Secret{
		Id:       s.Id,
		CreateAt: s.CreateAt,
		Spec:     s.CreateSecretRequest,
	}
}
